#!/bin/sh

set +x  			# turn off the debuger

#########################################################################################################
# This script is invoked during the the system startup to update the system.	 			#
# The script is invoked with debug off. 								#
#													#
#	Name  :	checkUSBUpdate										#
#	Input : $1 = The path to the USB stick where are stored this script and the			#
#		     packages to install								#
#		$2 = The path to the folder used as the mount point for the on board			#
#		     flash memory where the packages will be installed	 				#
#		$3 = The name of the device where the messages for the user will be printed		#
#													#
#	Map structure on USB memory stick:								#
#	- u-boot: u-boot-a66-minimal-SOLO-512MBx2-XXXX_and_bootargs.imx					#
#	- filesystem: iMX6_FS_a66_Flash_vXX.tar.gz							#
#	- kernel: a66_kernel_3.0.35-XXXX.tar.gz								#
#	- QT Libraries: qt4.8.6_vXX.tar.gz								#
#													#
#########################################################################################################

VERSION="Key1_Rev10"
DESCRIPTION="System Key: "
INFO="System restore procedure for the board A66"

# Packages to install
UBOOT=u-boot-a66-minimal-SOLO-512MBx2-2013-ge3102af_and_bootargs.imx
FILESYSTEM=iMX6_FS_a66_Flash_v10.tar.gz
KERNEL=a66_kernel_and_modules_3.0.35-gd42dbcd.tar.gz
QTLIBS=qt4.8.6_v1.tar.gz

ERROR=0

ST="***********************************************"
printf "\n\n$ST\n" >$3
printf "** $DESCRIPTION$VERSION\n" >$3
printf "** $INFO\n" >$3
printf "$ST\n\n" >$3

cd $1

# Sanity check
if [ ! -f $UBOOT ]
then
	echo " $UBOOT does not exist" >$3
	ERROR=1
fi
if [ ! -f $FILESYSTEM ]
then
	echo " $FILESYSTEM does not exist" >$3
	ERROR=1
fi
if [ ! -f $KERNEL ]
then
	echo " $KERNEL does not exist" >$3
	ERROR=1
fi
if [ ! -f $QTLIBS ]
then
	echo " $QTLIBS does not exist" >$3
	ERROR=1
fi

if [ $ERROR -eq 0 ]; then

	echo " saving the touch screen calibration data" >$3
	if [ -f pointercal ]
	then
		rm pointercal
	fi
	# mount the onboard flash memory
	mount /dev/mmcblk0p1 $2
	if [ -f $2/etc/pointercal ]
	then
		cp $2/etc/pointercal .
	fi
	umount $2

	echo " updating the u-boot" >$3
	dd if=$UBOOT of=/dev/mmcblk0 bs=512 seek=2 skip=2

	echo " removing the previous installation" >$3
	mke2fs -T ext3 /dev/mmcblk0p1

	# mount the onboard flash memory
	mount /dev/mmcblk0p1 $2

	echo " updating the filesystem" >$3
	tar pzxvf $FILESYSTEM -C $2

	echo " updating the kernel" >$3
	tar pzxvf $KERNEL -C $2

	echo " updating the QT libraries" >$3
	tar pzxvf $QTLIBS -C $2/opt

	echo $VERSION >$2/app/SystemVersion.txt

	echo " restoring the touch screen calibration data" >$3
	if [ -f pointercal ]
	then
		cp pointercal $2/etc/pointercal
	fi

	MSG_FILE="$2/app/app/Settings/MessagesVM.xml"

	printf "\
<?xml version=\"1.0\"?>\n\
<Body>\n\
    <Messages>\n\
        <Message Id=\"3\">\n\
" >$MSG_FILE

	printf "\
            <MessageText LanguageId=\"1\" Text=\"$VERSION\"></MessageText>\n\
            <MessageText LanguageId=\"2\" Text=\"$VERSION\"></MessageText>\n\
            <MessageText LanguageId=\"3\" Text=\"$VERSION\"></MessageText>\n\
" >>$MSG_FILE

	printf "\
        </Message>\n\
    </Messages>\n\
</Body>\n\
" >>$MSG_FILE

	sync
	umount $2
	sync

	sleep 5
fi

printf "\n" > $3
if [ $ERROR -eq 0 ]; then
	printf "\033[32m Update done.\n Please, turn off the power supply, remove the USB drive and turn on the power supply again\033[0m" >$3
else
	printf "\033[31m Error updating the machine.\n Please, turn off the power supply, remove the USB drive, check the system and retry...\033[0m" > $3
fi
printf "\n\n" > $3

exit 0
